
********************************************************************************	 
cd $pathdata_processed
use sample_tirage, clear

//predicted exam grade in each topic
gen skr_hat = .
		
global level1 fagkode real_stp year_vg3 fylke_vg3 skole_foretak_vg3 
global level2 fagkode real_stp year_vg3 fylke_vg3
global level3 fagkode real_stp year_vg3 

foreach i of numlist 1/3 {
	bysort ${level`i'}: egen sum_skr = sum(real_skr)
	replace sum_skr = sum_skr - real_skr if !missing(real_skr)
	bysort ${level`i'}: egen n = count(real_skr)
	replace n = n - 1 if !missing(real_skr)
	replace skr_hat = sum_skr/(n) if missing(skr_hat)	
	drop sum_skr n
	}

replace skr_hat = real_stp if missing(skr_hat) 

cd $pathdata_processed
save tirage_temp, replace
********************************************************************************

levelsof year_vg3, c
foreach year in `r(levels)' {
	cd $pathdata_processed
	use if year_vg3 == `year' using tirage_temp, clear

	drop if nb_exam == 0 
	
	sort nb_exam nb_kurs npid fagkode
	egen group = group(nb_kurs nb_exam)

	save tirage_temp`year', replace
	}
